package com.jelly.web.modules.system.dept.controller;

import com.jelly.common.constant.Constant;
import com.jelly.common.constant.GlobalConstant;
import com.jelly.common.note.OperationLog;
import com.jelly.common.util.StringUtils;
import com.jelly.common.util.web.AjaxPostResponse;
import com.jelly.web.share.base.controller.BaseController;
import com.jelly.web.share.base.domain.Base;
import com.jelly.web.modules.system.dept.domain.Dept;
import com.jelly.web.modules.system.dept.service.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @创建人 ql
 * @创建时间 2018-08-22 15:59
 * @项目名称 jelly
 * @功能描述: 部门控制器
 **/
@Controller
@RequestMapping("/admin/system/dept")
public class DeptController extends BaseController {

    @Autowired
    private IDeptService deptService;

    /**
     * 部门列表跳转
     * @return
     */
    @GetMapping
    public String getDeptList(){

        //获取字典详情
        getDictionary();

        //获取该用户可访问的按钮详情
        getButton();

        return adminSystemDept("/list");

    }

    /**
     * 部门列表详情
     * @param dept
     * @return
     */
    @PostMapping
    @ResponseBody
    public List<Dept> getDeptList(Dept dept){

        return deptService.getDeptList(dept);

    }

    /**
     * 修改部门跳转
     * @param model
     * @param base
     * @return
     */
    @GetMapping("/update")
    public String editDept(Model model, Base base){
        //获取字典详情
        getDictionary();

        model.addAttribute("deptList",deptService.getDeptList(Constant.SUCCESS));

        if(StringUtils.isNotBlank(base.getBaseId())){
            model.addAttribute("dept",deptService.getDeptId(base.getBaseId()));
        }
        return adminSystemDept("/update");

    }

    /**
     * 修改部门
     * @param dept
     * @return
     */
    @PostMapping("/update")
    @OperationLog(name = "部门模块",value = GlobalConstant.UPDATE)
    @ResponseBody
    public ResponseEntity<AjaxPostResponse> editDept(Dept dept){

        if(deptService.updateDept(dept)){
            return okResponse("修改成功");
        }

        return errorResponse("修改失败");

    }


    /**
     * 新增部门跳转
     * @param model
     * @return
     */
    @GetMapping("/add")
    public String addDept(Model model){
        //获取字典详情
        getDictionary();

        model.addAttribute("deptList",deptService.getDeptList(Constant.SUCCESS));

        return adminSystemDept("/add");
    }

    /**
     * 新增部门
     * @param dept
     * @return
     */
    @PostMapping("/add")
    @OperationLog(name = "部门模块",value = GlobalConstant.ADDED)
    @ResponseBody
    public ResponseEntity<AjaxPostResponse> addDept(Dept dept){

        if(deptService.addDept(dept)){
            return okResponse("新增成功");
        }
        return errorResponse("新增失败");

    }

    /**
     * 删除部门
     * @param base
     * @return
     */
    @PostMapping("/delect")
    @OperationLog(name = "部门模块",value = GlobalConstant.DELECT)
    @ResponseBody
    public ResponseEntity<AjaxPostResponse> delectDept(Base base){

        if(StringUtils.isNotBlank(base.getBaseId())){
            if(deptService.delectDept(base.getBaseId())){
                return okResponse("删除成功");
            }
        }

        return errorResponse("删除失败");

    }

    /**
     * 获取树结构
     * @param model
     * @return
     */
    @GetMapping("/level")
    public String treePermission(Model model){
        model.addAttribute("tree",deptService.getTreeDept());
        return adminSystemDept("/level");
    }

    /**
     * 获取树结构
     * @return
     */
    @PostMapping("/deptTree")
    @ResponseBody
    public ResponseEntity<AjaxPostResponse> getDeptTree(){
        return okResponse(deptService.getTreeDept());
    }

}
